#include <iostream>
#include <fstream>
#include <stdlib.h> //system("PAUSE")

#include "menu.h"

using namespace std;


Menu::Menu() : playerChar(NULL)
{
    //null;
}

Menu::~Menu()
{
    if (playerChar) delete playerChar;
}

void Menu::Run()
{
    //initializes the game
    writeMenu();
    char ans;
    bool b = false;
    while (!b)
    {
        cin >> ans;
        switch (ans)
        {
        case 'n' : case 'N' :
            {
                b = true;
                newGame();
                break;
            }
        case 'l' : case 'L' :
            {
                b = true;
                loadGame();
                break;
            }
        default :
            {
                cout << "not valid answer, please try again" << endl;
                cin.clear(); while (cin.peek() != '\n') cin.ignore();
            }
        }
    }
    quitGame();
}

void Menu::writeMenu()
{
    // displays starting text
    cout << "Welcome to the game." << endl;
    cout << "Press 'n' to start a new game" << endl;
    cout << "Press 'l' to load an existing game" << endl;
}

void Menu::newGame()
{   // initializes a new game
    cin.clear(); while (cin.peek() != '\n') cin.ignore();
    string cname;
    cout << "Give a name for your hero:\t";
    cin.ignore();       // this removes a \n left from cin>>
    getline(cin, cname);
    cout << endl;

    cout << "Choose a class: " << endl;
    cout << "\tWrite 'm' for Mage (very high attack, low defense)" << endl;
    cout << "\tWrite 'r' for Rogue (fast, medium defenses)" << endl;
    cout << "\tWrite 'w' for Warrior (strong defense, weak offense)" << endl;
    char cchar;
    bool b = false;
    while (!b)
    {
        cin >> cchar;
        switch (cchar)
        {
        case 'm' : case 'M' :
            {
                b = true;
                playerChar = new Mage(cname);
                break;
            }
        case 'r' : case 'R' :
            {
                b = true;
                playerChar = new Rogue(cname);
                break;
            }
        case 'w' : case 'W' :
            {
                b = true;
                playerChar = new Warrior(cname);
                break;
            }
        default :
            {
                cout << "not valid answer, please try again" << endl;
                cin.clear(); while (cin.peek() != '\n') cin.ignore();
            }
        }
    }
    cin.clear(); while (cin.peek() != '\n') cin.ignore();
    game.setPlayer(playerChar);
    playerChar->writeInfo();

    ifstream missionFile("m1.mission");
    if (missionFile.fail())
    {
        cout << "The file 'm1.mission' is missing, please move it to the same location where 'rpg.exe' is." << endl;
    } else
    {
        game.readMissions(missionFile);
        missionFile.close();
        game.nextMission(1);
    }
}

void Menu::loadGame()
{
    // loads a game in progress from a save
    cin.clear(); while (cin.peek() != '\n') cin.ignore();
    string saveF;
    ifstream saveFile;

    cout << "Provide the name of the save file you wish to load:" << endl;
    cin >> saveF;
    saveF.append(".save");

    saveFile.open(saveF.c_str());
    while (saveFile.fail())
        {
            cout << "File not found. Try again:" << endl;
            cin.clear(); while (cin.peek() != '\n') cin.ignore();
            cin >> saveF;
            saveF.append(".save");
            saveFile.open(saveF.c_str());
        }
    cin.clear(); while (cin.peek() != '\n') cin.ignore();
    // build up the game (character, missions) based on the save data
    string name;
    int classID;
    int experience;
    int currentMission;
    getline(saveFile, name);
    saveFile >> classID >> experience >> currentMission;
    switch (classID)
    {
    case 1:
        {
            playerChar = new Warrior(name);
            break;
        }
    case 2:
        {
            playerChar = new Rogue(name);
            break;
        }
    case 3:
        {
            playerChar = new Mage(name);
            break;
        }
    }
    playerChar->gainExperience(experience);
    game.setPlayer(playerChar);
    game.readMissions(saveFile);
    saveFile.close();
    game.nextMission(currentMission);
}

void Menu::quitGame()
{
    // ends the program
    cout << "Thank you for playing!" << endl;
    system("PAUSE");
}
